#pragma once

#include <map>
#include <set>

#include <leveldb/db.h>
#include <leveldb/write_batch.h>

#include "Message.h"


namespace PC2
{

class TransactionInfoSet
{
public:
	Trans m_LastTrans;
	std::string m_TransData;
	std::set<uint32_t> m_RelatedNodes;
	// for coordinator
	std::map<uint32_t, Trans> m_PrepareResponses;
};


class PC2DataStore
{
public:
	PC2DataStore(char const* apPath);
	virtual ~PC2DataStore();

public:
	leveldb::Iterator* CreateIterator();

public:
	static void WriteCurrentTransId(leveldb::WriteBatch* apBatch, uint32_t aTransId);
	static void WriteTransData(leveldb::WriteBatch* apBatch, TransactionInfoSet* apInfoSet);
	static void WriteLastTransState(leveldb::WriteBatch* apBatch, TransactionInfoSet* apInfoSet);
	static void WriteRelatedNodeSet(leveldb::WriteBatch* apBatch, TransactionInfoSet* apInfoSet);
	static void WritePrepareResponseSet(leveldb::WriteBatch* apBatch, TransactionInfoSet* apInfoSet);
	static void FinishAndClearTransactionInfo(leveldb::WriteBatch* apBatch, TransactionInfoSet* apInfoSet);

public:
	uint32_t GetCurrentTransId();
	bool LoadTranInfoSet(uint32_t aTransID, TransactionInfoSet* apSet, bool aCollectResponse);

public:
	void CommitBatch(leveldb::WriteBatch* apBatch);

protected:
	static void WriteResponseSet(char const* apKey, leveldb::WriteBatch* apBatch, uint32_t aTransID, std::map<uint32_t, Trans>* apSet);
	bool GetResponseSet(char const* apKey, uint32_t aTransID, std::map<uint32_t, Trans>* apSet);

protected:
	leveldb::DB* m_pDB;
};




}